// pages/Users/UserInfo/UserInfo.js
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { getUser , updateUser } from '../../../api/user';
import { ACCESS_TOKEN } from '../../../utils/types';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    showPrivacyAuthor: false,
    ip: app.globalData.global.http,
    user: {
      userId: 0,
      realName: '',
      phone: '',
      nickname: '',
      headimgurl: '',
      grade: 0,
      aliAccount: '',
    },
    focus: false,
    isReceiver: '0', // 是否是接单员
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var _this = this;
    if (wx.onNeedPrivacyAuthorization) {
      wx.onNeedPrivacyAuthorization((resolve) => {
        console.log('触发弹窗')
        // 触发弹窗
        _this.setData({
          showPrivacyAuthor: true,
        })
        _this.resolvePrivacyAuthorization = resolve
      })
    }
    this.getUser();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 1;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  handleAgreePrivacyAuthorizationFun(){
    console.log('同意之后的回调');
    // this.getUserAddress();
  },
  disagreePrivacyAuthorization(){
    this.resolvePrivacyAuthorization({ event: 'disagree' });
  },
  // 获取用户信息
  getUser(){
    let _this = this;
    getUser({})
    .then(res => {
      if(res.success){
        _this.setData({
          user: res.data,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 设置用户头像
  changeUserInfoAvatarUrl(e){
    console.log(e,'e')
    this.setUserHeadImg(e.detail.avatarUrl)
  },
  setUserHeadImg(imgUrl){
    let _this = this
    var token = wx.getStorageSync(ACCESS_TOKEN)
    wx.getImageInfo({
      src: imgUrl,
      success: function(res){
        console.log(res,'res')
        wx.uploadFile({
          url: _this.data.ip + '/app/upload/user/headimg',
          filePath: res.path,
          name: 'headimg',
          formData: {
            'access_token': token,
          },
          success: function(res){
            console.log(res,'上传图片res')
            // if(res.success){
            // }
            _this.getUser();
          },
          fail: function(err){
            console.log(err)
          },
        })
      }
    })
  },
  // 设置昵称
  nickNameIptBlur(){
    this.setData({
      focus: true,
    })
  },
  inputWord(e){
    console.log(e)
    let _this = this;
    wx.createSelectorQuery().in(this) // 注意这里要加上 in(this)
    .select(".userNicknamIpt")
    .fields({
        properties: ["value"],
    })
    .exec((res) => {
      var nickName = ''
      if(res.length > 0){
        nickName = res[0].value
      }
      console.log('昵称', res)
      if(nickName != ''){
        _this.setUserNickName(nickName)
      }
    })
  },
  setUserNickName(nickName){
    let _this = this
    var params = {
      nickname: nickName,
    }
    // if(!_this.data.user.provnice){
    //   params.province = userInfo.province;
    //   params.city = userInfo.city;
    //   params.country = userInfo.country;
    // }
    return new Promise((resolve,reject) => {
      updateUser(params)
      .then(res=>{
        if(res.success){
          _this.getUser();
          resolve();
        }else{
          reject(res.errMessage)
        }
      })
    })
  },
  // 页面跳转
  goNext(e){
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    // return
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}`,
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}`,
        })
      }
      
    }
  },
})